<?php
App::uses('AppController', 'Controller');

/**
 *author quoctran
 * 
 */
class PostsController extends AppController {
	var $name = "Posts";// ten cua Controller
    var $helpers = array('Paginator','Html');
    var $components = array('Session', 'Link');
    var $paginate = array();
	public function beforeFilter() {
		parent::beforeFilter();
		$uploadURL = Router::url('/') . 'app/webroot/files';
		$_SESSION['KCFINDER'] = array(
			'disabled' => false,
			'uploadURL' => $uploadURL,
			'uploadDir' => ''
		);
		parent::getTopic();
		$this->Auth->allow(array("search"));
	}

	/**
	 * index method
	 */
	public function index() {
		$conditions = array("Post.deleted" => 0);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("Post",$conditions);
        $this->set("data",$data);
		$this->set("strTitle","Bài viết");
	}

	/**
	 * add method
	 */
	public function add() {
		$this->set("strTitle","Thêm Bài viết");
		$this->loadModel('Category');
		$this->loadModel('User');
		
		$cat = $this->Category->find('all');
		$this->set("cats", $cat);
		
		$uid = CakeSession::read("Auth.User.id");
		if ($this -> request -> is('post')) {
			
			$new = $this -> data;
			$new['deleted'] = 0;
			$new['updateAt'] = date("Y-m-d H:i:s");
			$new['createAt'] = date("Y-m-d H:i:s");
			$new['createBy'] = $uid;
			$new['shortLink'] = $this->Link->getShortLink($new['title']);
			if ($this -> Post -> save($new)){
				$this->Session->setFlash('Thêm thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
		
	}

	/**
	 * edit method
	 * param id integer , id of product
	 */
	public function edit($id) {
		$this->set("strTitle","Chỉnh sửa Bài viết");
		$this->loadModel('Category');
		$this->loadModel('User');
		
		$cat = $this->Category->find('all');
		$this->set("cats", $cat);
		
		$conditions = array("Post.id" => $id);
		$data = $this->Post->find('first', array('conditions' => $conditions));
		$this -> set("data", $data);
		
		
		$uid = CakeSession::read("Auth.User.id");
		if ($this -> request -> is('post')) {
			
			$new = $this -> data;
			$new['deleted'] = 0;
			$new['updateAt'] = date("Y-m-d H:i:s");
			$new['createBy'] = $uid;
			//$new['shortLink'] = $this->Link->getShortLink($new['title']);
			if ($this -> Post -> save($new)){
				$this->Session->setFlash('Thêm thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
	}

	/**
	 * delete method
	 * param id integer , id of product
	 */
	public function delete($id) {
		$conditions = array("Product.id" => $id);
		$data = $this->Product->find('first', array('conditions' => $conditions));
		$data['Product']['deleted'] = 1;
		$data['Product']['updateAt'] = date("Y-m-d H:i:s"); 
		if($this -> Product -> save($data)){
			$this->Session->setFlash('Sảm phẩm "'.$data['Product']['name'].'" đã bị xóa');
		}
		return $this->redirect(array('controller' => 'Products', 'action' => 'index'));
	}
	public function search(){
		$this->set("strTitle","Kết quả tìm kiếm");
		$search_text = '';
		if ($this -> request -> is('post')) {
			$search_text = $this->data['keyword'];
		} 		
		
		$conditions = array('Post.title LIKE' => "%". $search_text ."%", "Post.deleted"=>0);
		$this -> paginate = array(
           'limit' => ITEM_PER_PAGE_USER,
            'order' => array('Post.id' => 'asc'),
        );
		$data = $this -> paginate("Post",$conditions);

		$this -> set("search_text", $search_text);
		$this -> set("data",$data);
		$this->set("title_for_user","Tìm kiếm");
		
	}

}
?>